package vcproj2cmake.writer;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import jtools.files.TextFile;
import vcproj2cmake.Configuration;
import vcproj2cmake.Solution;

public class SolutionWriter {

	
	public String writeSolutionToString(Solution solution){
		String output="";

		
		output += "PROJECT("+solution.name+")\n";
		
		output += "\ncmake_minimum_required(VERSION 2.4)\n\n";
		output += "if(MSVC) \nset(CMAKE_USE_RELATIVE_PATHS ON CACHE INTERNAL \"\" FORCE)\nendif()\n\n";
		
		output += "SET( ROOT_DIRECTORY \""+solution.rootPath.getAbsolutePath().replace("\\", "/") + "\" CACHE PATH \" Where is the root directory\" )\n\n";
		
		Iterator<String> iter = solution.pathMap.keySet().iterator();
		while(iter.hasNext()){
			String k = iter.next();
			File v = solution.pathMap.get(k);
			v = trimFile(v);
			String p = v.getAbsolutePath().replace(solution.rootPath.getAbsolutePath(), "${ROOT_DIRECTORY}");
			p = p.replace("\\", "/");
			output += "SET( " + k + " \"" + p + "\" CACHE PATH \" Where to find <"+k+">\" FORCE )\n";
		}
	
		output += "\nSET(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib CACHE PATH \"Output directory for libraries\" )\n";
		output += "SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin CACHE PATH \"Output directory for applications\" )\n\n";
		//output += "SET(OBJECT_DIR ../../objs CACHE PATH \"\")\n";
		
		output +="\n\n\n # add Solution global Variables here\n\n\n";
		
		String root = solution.rootPath.getAbsolutePath().replace("\\", "/");
		for (int i = 0; i < solution.projects.size(); i++){
			String pf = solution.projects.get(i).projFile.getParentFile().getAbsolutePath().replace("\\", "/");
			pf = pf.replace(root, "").substring(1);
			output += "ADD_SUBDIRECTORY ( " + pf + ")\n";
		}
		return output;
	}
	
	private File trimFile(File v) {
		try {
			String p = v.getCanonicalPath();
			return new File(p);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	public void writeSolutionToCMakeLists(Solution solution, String string) throws IOException{
		String solutionText = writeSolutionToString(solution);
		TextFile tf = new TextFile();
		tf.add(solutionText);
		tf.writeFile(solution.rootPath.getAbsolutePath()+"/CMakeLists.txt", true);
		tf.closeFile();
		for (int i = 0; i < solution.projects.size(); i++){
			Configuration config = null;
			for (int j = 0; j < solution.projects.get(i).configurations.size(); j++){
				if (solution.projects.get(i).configurations.get(j).name.equals(string))
					config = solution.projects.get(i).configurations.get(j);
			}
			if (config == null)
				continue;
			String projText = new cmakeWriter().writeConfigurationToString(solution.projects.get(i), config, solution.file.getParentFile());
			File f = solution.projects.get(i).projFile;
			tf = new TextFile();
			tf.add(projText);
			tf.writeFile(f.getParent()+"/CMakeLists.txt", true);
			tf.closeFile();
		}
	}
}
